/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



// Floor platform for springs

//# address-hook(0x060d58) end(0x060d78)
function void fn060d58()
{
	setupObjectAttributesFull(0x061398)

	objA0.position.x.u16 = 0x0100
	objA0.position.y.u16 = 0x0310

	// "spawnChildObjects(0x061424)" replaced by:
	spawnChildObject(0x060dac, 0x00, -48, -24)		// Single spring
	spawnChildObject(0x060dac, 0x02, -16, -24)		// Single spring
	spawnChildObject(0x060dac, 0x04, 16, -24)		// Single spring
	spawnChildObject(0x060dac, 0x06, 48, -24)		// Single spring

	objA0.update_address = 0x060d7e
	fn060d7e()
}


//# address-hook(0x060d7e) end(0x060da6)
function void fn060d7e()
{
	if ((objA0.flags38 & 0x02) == 0)
	{
		A1 = 0x01dc56
		fn061314()
		DrawObject()
	}
	else
	{
		u8[0xffff2022] = 0

		// "spawnSimpleChildObjects(0x06143e)" replaced by:
		spawnSimpleChildObjects(0x06101e, 16)		// Collapsing spring platform debris

		Object.TriggerUnloading()
	}
}



// Single spring

//# address-hook(0x060dac) end(0x060e46)
function void fn060dac()
{
	setupObjectAttributesFull(0x0613c8)
	u16[A0 + 0x30] = 0xf000
	A1 = 0xffff0000 + u16[A0 + 0x46]
	if ((u8[A1 + 0x38] & 0x02) == 0)
	{
		D1.u16 = 0x1b
		D2.u16 = 0x08
		D3.u16 = 0x10
		D4.u16 = objA0.position.x.u16

		push(D4)
		push(D3)
		push(D2)
		push(D1)

		A1 = 0xffffb000
		D6 = 3		// object.flag.P1_ON_OBJECT
		fn01dccc()

		if (objA0.flags2a & object.flag.P1_ON_OBJECT)
		{
			objA0.flags38 |= 0x20
			SpringVertical.ApplyToCharacter()
		}

		D1 = pop()
		D2 = pop()
		D3 = pop()
		D4 = pop()

		A1 = 0xffffb04a
		D6 = 4		// object.flag.P2_ON_OBJECT
		fn01dccc()

		if (objA0.flags2a & object.flag.P2_ON_OBJECT)
		{
			SpringVertical.ApplyToCharacter()
		}

		Object.animationUpdate(0x023722)

		if ((objA0.flags38 & 0x20) == 0 || objA0.state.former != 0x01)
		{
			DrawObject()
			return
		}
	}

	objA0.update_address = 0x0856d0
	A1 = 0xffff0000 + u16[A0 + 0x46]
	u8[A1 + 0x38] |= 0x02
	DrawObject()
}



// Used by floor platform

//# address-hook(0x061314) end(0x06131e)
function void fn061314()
{
	D1 = 0x4b
	D2 = 0x10
	D3 = 0x11
	D4.u16 = objA0.position.x.u16
	call A1
}
